---
layout: "default"
title: "Dictionary"
description: "Swift documentation for 'Dictionary': A hash-based mapping from Key to Value instances.  Also a."
keywords: "Dictionary,struct,swift,documentation,generate,getMirror,indexForKey,removeAll,removeAtIndex,removeValueForKey,updateValue,count,debugDescription,description,endIndex,isEmpty,keys,startIndex,values,Element,Index,Generator,SubSequence"
root: "/v1.2"
---

<div class="intro-declaration"><code class="language-swift">struct Dictionary&lt;Key : Hashable, Value&gt;</code></div>

<div class="discussion comment">
    <p>A hash-based mapping from <code>Key</code> to <code>Value</code> instances.  Also a
collection of key-value pairs with no defined ordering.</p>
</div>

<table class="standard">
<tr>
<th id="inheritance">Inheritance</th>
<td>
<code class="inherits">CollectionType, DebugPrintable, DictionaryLiteralConvertible, Printable, Reflectable, SequenceType, _CollectionType, _SequenceType, _Sequence_Type</code>
<span class="viz"><a href="hierarchy/">View Protocol Hierarchy &rarr;</a></span>
</td>
</tr>

<tr>
<th id="aliases">Associated Types</th>
<td>
<span id="aliasesmark"></span>
<div class="declaration">
<code class="language-swift">Element = (Key, Value)</code>
</div>
<div class="declaration">
<code class="language-swift">Index = DictionaryIndex&lt;Key, Value&gt;</code>
</div>
<div class="declaration">
<code class="language-swift">Generator = DictionaryGenerator&lt;Key, Value&gt;</code>
<div class="comment">
    <p><em>Type alias inferred.</em></p>
</div>
</div>
<div class="declaration">
<code class="language-swift">SubSequence = Slice&lt;Dictionary&lt;Key, Value&gt;&gt;</code>
<div class="comment">
    <p><em>Type alias inferred.</em></p>
</div>
</div>
</td>
</tr>


<tr>
<th>Import</th>
<td><code class="language-swift">import Swift</code></td>
</tr>

</table>


<h3>Initializers</h3>
<div class="declaration" id="init">
<a class="toggle-link" data-toggle="collapse" href="#comment-init">init()</a><div class="comment collapse" id="comment-init"><div class="p">
    <p>Create an empty dictionary.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init()</code>

    </div></div>
</div>
<div class="declaration" id="init-dictionaryliteral_">
<a class="toggle-link" data-toggle="collapse" href="#comment-init-dictionaryliteral_">init(<wbr>dictionaryLiteral:)</a><div class="comment collapse" id="comment-init-dictionaryliteral_"><div class="p">
    <p>Create an instance initialized with <code>elements</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init(dictionaryLiteral elements: (Key, Value)...)</code>

    </div></div>
</div>
<div class="declaration" id="init-minimumcapacity_">
<a class="toggle-link" data-toggle="collapse" href="#comment-init-minimumcapacity_">init(<wbr>minimumCapacity:)</a><div class="comment collapse" id="comment-init-minimumcapacity_"><div class="p">
    <p>Create a dictionary with at least the given number of
elements worth of storage.  The actual capacity will be the
smallest power of 2 that&#39;s &gt;= <code>minimumCapacity</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init(minimumCapacity: Int)</code>

    </div></div>
</div>


<h3>Instance Variables</h3>
<div class="declaration" id="var-count_-int">
<a class="toggle-link" data-toggle="collapse" href="#comment-var-count_-int">var count: Int</a><div class="comment collapse" id="comment-var-count_-int"><div class="p">
    <p>The number of entries in the dictionary.</p>

<p>Complexity: O(1)</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var count: Int { get }</code>

    </div></div>
</div>
<div class="declaration" id="var-debugdescription_-string">
<a class="toggle-link" data-toggle="collapse" href="#comment-var-debugdescription_-string">var debugDescription: String</a><div class="comment collapse" id="comment-var-debugdescription_-string"><div class="p">
    <p>A textual representation of <code>self</code>, suitable for debugging.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var debugDescription: String { get }</code>

    </div></div>
</div>
<div class="declaration" id="var-description_-string">
<a class="toggle-link" data-toggle="collapse" href="#comment-var-description_-string">var description: String</a><div class="comment collapse" id="comment-var-description_-string"><div class="p">
    <p>A textual representation of <code>self</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var description: String { get }</code>

    </div></div>
</div>
<div class="declaration" id="var-endindex_-dictionaryindex-key-value">
<a class="toggle-link" data-toggle="collapse" href="#comment-var-endindex_-dictionaryindex-key-value">var endIndex: DictionaryIndex&lt;Key, Value&gt;</a><div class="comment collapse" id="comment-var-endindex_-dictionaryindex-key-value"><div class="p">
    <p>The collection&#39;s &quot;past the end&quot; position.</p>

<p><code>endIndex</code> is not a valid argument to <code>subscript</code>, and is always
reachable from <code>startIndex</code> by zero or more applications of
<code>successor()</code>.</p>

<p>Complexity: amortized O(1) if <code>self</code> does not wrap a bridged
<code>NSDictionary</code>, O(N) otherwise.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var endIndex: DictionaryIndex&lt;Key, Value&gt; { get }</code>

    </div></div>
</div>
<div class="declaration" id="var-isempty_-bool">
<a class="toggle-link" data-toggle="collapse" href="#comment-var-isempty_-bool">var isEmpty: Bool</a><div class="comment collapse" id="comment-var-isempty_-bool"><div class="p">
    <p>True iff <code>count == 0</code></p>

    <h4>Declaration</h4>    
    <code class="language-swift">var isEmpty: Bool { get }</code>

    </div></div>
</div>
<div class="declaration" id="var-keys_-lazyforwardcollection-mapcollectionview-key_-value-key">
<a class="toggle-link" data-toggle="collapse" href="#comment-var-keys_-lazyforwardcollection-mapcollectionview-key_-value-key">var keys: LazyForwardCollection&lt;MapCollectionView&lt;[Key : Value], Key&gt;&gt;</a><div class="comment collapse" id="comment-var-keys_-lazyforwardcollection-mapcollectionview-key_-value-key"><div class="p">
    <p>A collection containing just the keys of <code>self</code></p>

<p>Keys appear in the same order as they occur as the <code>.0</code> member
of key-value pairs in <code>self</code>.  Each key in the result has a
unique value.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var keys: LazyForwardCollection&lt;MapCollectionView&lt;[Key : Value], Key&gt;&gt; { get }</code>

    </div></div>
</div>
<div class="declaration" id="var-startindex_-dictionaryindex-key-value">
<a class="toggle-link" data-toggle="collapse" href="#comment-var-startindex_-dictionaryindex-key-value">var startIndex: DictionaryIndex&lt;Key, Value&gt;</a><div class="comment collapse" id="comment-var-startindex_-dictionaryindex-key-value"><div class="p">
    <p>The position of the first element in a non-empty dictionary.</p>

<p>Identical to <code>endIndex</code> in an empty dictionary</p>

<p>Complexity: amortized O(1) if <code>self</code> does not wrap a bridged
<code>NSDictionary</code>, O(N) otherwise.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var startIndex: DictionaryIndex&lt;Key, Value&gt; { get }</code>

    </div></div>
</div>
<div class="declaration" id="var-values_-lazyforwardcollection-mapcollectionview-key_-value-value">
<a class="toggle-link" data-toggle="collapse" href="#comment-var-values_-lazyforwardcollection-mapcollectionview-key_-value-value">var values: LazyForwardCollection&lt;MapCollectionView&lt;[Key : Value], Value&gt;&gt;</a><div class="comment collapse" id="comment-var-values_-lazyforwardcollection-mapcollectionview-key_-value-value"><div class="p">
    <p>A collection containing just the values of <code>self</code></p>

<p>Values appear in the same order as they occur as the <code>.1</code> member
of key-value pairs in <code>self</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">var values: LazyForwardCollection&lt;MapCollectionView&lt;[Key : Value], Value&gt;&gt; { get }</code>

    </div></div>
</div>

<h3>Subscripts</h3>
<div class="declaration" id="subscript-subscript_-key">
<a class="toggle-link" data-toggle="collapse" href="#comment-subscript-subscript_-key">subscript(_: Key)</a>
<div class="comment collapse" id="comment-subscript-subscript_-key"><div class="p">
    

    <h4>Declaration</h4>    
    <code class="language-swift">subscript(key: Key) -&gt; Value?</code>
    
    
</div></div>
</div>
<div class="declaration" id="subscript-subscript_-dictionaryindex-key-value">
<a class="toggle-link" data-toggle="collapse" href="#comment-subscript-subscript_-dictionaryindex-key-value">subscript(_: DictionaryIndex&lt;Key, Value&gt;)</a>
<div class="comment collapse" id="comment-subscript-subscript_-dictionaryindex-key-value"><div class="p">
    

    <h4>Declaration</h4>    
    <code class="language-swift">subscript(position: DictionaryIndex&lt;Key, Value&gt;) -&gt; (Key, Value) { get }</code>
    
    
</div></div>
</div>


<h3>Instance Methods</h3>
<div class="declaration" id="func-generate">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-generate">func generate()</a>
        
<div class="comment collapse" id="comment-func-generate"><div class="p">
    <p>Return a <em>generator</em> over the (key, value) pairs.</p>

<p>Complexity: O(1)</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func generate() -&gt; DictionaryGenerator&lt;Key, Value&gt;</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-getmirror">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-getmirror">func getMirror()</a>
        
<div class="comment collapse" id="comment-func-getmirror"><div class="p">
    <p>Returns a mirror that reflects <code>self</code>.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func getMirror() -&gt; MirrorType</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-indexforkey_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-indexforkey_">func indexForKey(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-indexforkey_"><div class="p">
    <p>Returns the <code>Index</code> for the given key, or <code>nil</code> if the key is not
present in the dictionary.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">func indexForKey(key: Key) -&gt; DictionaryIndex&lt;Key, Value&gt;?</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-removeall_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-removeall_">mutating func removeAll(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-removeall_"><div class="p">
    <p>Remove all elements.</p>

<p>Postcondition: <code>capacity == 0</code> if <code>keepCapacity</code> is <code>false</code>, otherwise
the capacity will not be decreased.</p>

<p>Invalidates all indices with respect to <code>self</code>.</p>

<p><strong>keepCapacity</strong> If <code>true</code>, the operation preserves the
storage capacity that the collection has, otherwise the underlying
storage is released.  The default is <code>false</code>.</p>

<p>Complexity: O(<code>count</code>).</p>

    <h4>Declaration</h4>    
    <code class="language-swift">mutating func removeAll(keepCapacity: Bool = default)</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-removeatindex_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-removeatindex_">mutating func removeAtIndex(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-removeatindex_"><div class="p">
    <p>Remove the key-value pair at index <code>i</code></p>

<p>Invalidates all indices with respect to <code>self</code>.</p>

<p>Complexity: O(<code>count</code>).</p>

    <h4>Declaration</h4>    
    <code class="language-swift">mutating func removeAtIndex(index: DictionaryIndex&lt;Key, Value&gt;)</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-removevalueforkey_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-removevalueforkey_">mutating func removeValueForKey(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-removevalueforkey_"><div class="p">
    <p>Remove a given key and the associated value from the dictionary.
Returns the value that was removed, or <code>nil</code> if the key was not present
in the dictionary.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">mutating func removeValueForKey(key: Key) -&gt; Value?</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-updatevalue_forkey_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-updatevalue_forkey_">mutating func updateValue(<wbr>_:<wbr>forKey:)</a>
        
<div class="comment collapse" id="comment-func-updatevalue_forkey_"><div class="p">
    <p>Update the value stored in the dictionary for the given key, or, if they
key does not exist, add a new key-value pair to the dictionary.</p>

<p>Returns the value that was replaced, or <code>nil</code> if a new key-value pair
was added.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">mutating func updateValue(value: Value, forKey key: Key) -&gt; Value?</code>
    
    
</div></div>
</div>


